package com.chen.gulimall.gulimallproduct.controller;

import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.chen.gulimall.gulimallproduct.entity.AttrEntity;
import com.chen.gulimall.gulimallproduct.service.AttrService;
import com.chen.gulimall.gulimallproduct.service.CategoryService;
import com.chen.gulimall.gulimallproduct.vo.AttrGroupWithAttrVo;
import com.chen.gulimall.gulimallproduct.vo.AttrRelationVo;
import com.chen.gulimallcommon.common.utils.PageUtils;
import com.chen.gulimallcommon.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.chen.gulimall.gulimallproduct.entity.AttrGroupEntity;
import com.chen.gulimall.gulimallproduct.service.AttrGroupService;




/**
 * 属性分组
 *
 * @author mr.chen
 * @email 3061634906@qq.com
 * @date 2022-04-18 22:06:56
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catlogId}")
    //@RequiresPermissions("gulimallproduct:attrgroup:list")
    //得到路径中的参数值
    public R list(@RequestParam Map<String, Object> params,@PathVariable Long catlogId){
        PageUtils page = attrGroupService.queryPage(params,catlogId);

        return R.ok().put("page", page);
    }



    @GetMapping("/{attrgroupId}/attr/relation")
    public R getAttrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
         List<AttrEntity> data = attrService.getAttrRelation(attrgroupId);

         return R.ok().put("data",data);
    }

    @GetMapping("/{attrgroupId}/noattr/relation")
    public R getAttrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.getAttrNoRelation(attrgroupId,params);

        return R.ok().put("page",page);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("gulimallproduct:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
		Long[] path = categoryService.findCatelogPath(attrGroup.getCatelogId());
		attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("gulimallproduct:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("gulimallproduct:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/attr/relation/delete")
    //@RequiresPermissions("gulimallproduct:attrgroup:delete")
    public R delete(@RequestBody AttrRelationVo[] vos){
		attrGroupService.deleteRelation(vos);

        return R.ok();
    }

    @RequestMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttr(@PathVariable("catelogId") Long catelogId) {
        List<AttrGroupWithAttrVo> data = attrGroupService.getAttrGroupWithAttr(catelogId);

        return R.ok().put("data",data);
    }

}
